package com.domain.controller;

import com.domain.common.AppLogger;
import com.domain.common.Comments;
import com.domain.common.Result;
import com.domain.pojo.SmsCode;
import com.domain.pojo.Users;
import com.domain.service.SmsCodeService;
import com.domain.service.LoginService;
import com.domain.utils.NotNullUtils;
import com.domain.utils.RandomUtils;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * 登录 注册控制器
 */
@Controller
@RequestMapping("/login")
public class LoginController {
    /**
     * 日志
     */
    private static Logger logger = AppLogger.getLogger(LoginController.class.getSimpleName(), Level.INFO.toString(), true);

    @Autowired
    private SmsCodeService smsCodeService;

    @Autowired
    private LoginService loginService;


    /**
     * 获取短信验证码
     *
     * @param phoneNum //手机号码
     */
    @RequestMapping(value = "/getsmsCode", method = RequestMethod.GET)
    @ResponseBody
    public Result getsmsCode(String phoneNum) {
        try {
            //保存短信验证码信息
            String code = RandomUtils.getCode();
          /*  AliyunSmsUtils.sendSmsResgster(phoneNum, code);*/
            smsCodeService.saveSmsCode(phoneNum, code);
            return Result.success("验证码", code);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("短信验证码获取失败");
        }
    }

    /**
     * 获取 个人标签列表
     *
     * @return
     */
    @RequestMapping(value = "/getUserLable", method = RequestMethod.POST)
    @ResponseBody
    public Result getUserLable(String pageNum, String pageSize) {
        try {
            return loginService.getUserLable(pageNum, pageSize);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("系统错误");
        }
    }

    /**
     * 获取行业标签列表
     *
     * @return
     */
    @RequestMapping(value = "/getUserIndustrysList", method = RequestMethod.POST)
    @ResponseBody
    public Result getUserIndustrysList(String pageNum, String pageSize) {
        try {
            return loginService.getUserIndustrysList(pageNum, pageSize);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("系统错误");
        }
    }

    /**
     * 注册验证
     *
     * @param phoneNumber
     * @param
     * @param
     * @return
     */
    @RequestMapping(value = "/checkRegister", method = RequestMethod.POST)
    @ResponseBody
    public Result checkPhone(String phoneNumber, String smsCode, HttpServletRequest request) {
        try {
            //验证验证码码得时效性（五分钟）
            SmsCode sc = smsCodeService.checkSmsCodeTime(phoneNumber, smsCode);
            if (!NotNullUtils.isNotNullObject(sc)) {
                return Result.publicResult(Comments.SMSCODE_FAIL, "验证码错误");
            } else {
                Long s = (System.currentTimeMillis() - sc.getCreatTime().getTime()) / (1000 * 60);
                if (s >= 5) {
                    //过期删除验证码
                    smsCodeService.delect(sc.getId());
                    return Result.publicResult(Comments.SMSCODE_CHECK, "验证码已过期！");
                }
            }
            //验证手机号是否注册
            int resultCount = loginService.checkPhone(phoneNumber);
            Users users = new Users();
            if (resultCount > 0) {
                //删除验证码
                smsCodeService.delect(sc.getId());
                //保存用户信息到session
                users = loginService.checklogin(phoneNumber, request);
            } else {
                return Result.publicResult(Comments.ILLEGAL_ARGUMENT, "请注册");
            }
            return Result.success("操作成功", users);

        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("系统错误");
        }
    }


    /**
     * 注册
     *
     * @param phoneNumber   电话号码
     * @param name          真实姓名
     * @param gender        性别（1 男  2 女）
     * @param birthday      生日
     * @param city          城市名称
     * @param industry      职业
     * @param personalLabel 个人标签
     * @param headImage     头像
     * @param request
     * @return
     */
    @RequestMapping(value = "/register", method = RequestMethod.POST)
    @ResponseBody
    public Result register(
            String phoneNumber,
            String name,
            String gender,
            String birthday,
            String city,
            String industry,
            String personalLabel,
            String headImage,
            HttpServletRequest request) {
        try {
            Users users = loginService.saveUser(phoneNumber, name, gender, birthday, city, industry, personalLabel, headImage, request);
            return Result.success("操作成功", users);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("系统错误");
        }
    }
}
